import functools

from flask import (
    Blueprint, flash, g, redirect, render_template, request, session, url_for
)

bp = Blueprint('post_contents', __name__)

@bp.route('/home', methods=('GET', 'POST',))
def home_page():
    g.mid_vars={}
    if request.method == 'POST': # 用户已填写content
        abstract_content = request.form['content']
        error = None

        if not abstract_content:
            # g.abstract_content=None
            print('未输入摘要')
            error = 'The content of the abstract cannot be empty'

        if error is None:
            g.mid_vars['abstract_content']=abstract_content
            print(f"已接收用户输入:#{g.mid_vars['abstract_content']}#")
            g.mid_vars['category_result']=model_predict(abstract_content)
            print(f"文献分类结果:#{g.mid_vars['category_result']}#")

        else:
            flash(error)
    # get方法: 用户还未填表
    else:
        print('get方法')
        g.mid_vars['abstract_content']=None
        g.mid_vars['category_result']=None
        print('分类结果:',g.mid_vars['category_result'])
    return render_template('post_contents/home.html')

def model_predict(abstract_content:str)->str:
    # TODO: finish this part
    if len(abstract_content)<=5:
        return '测绘科学与技术'
    return '基础医学/临床医学'